//
//  RBNavigationController.m
//  Budejie
//
//  Created by 华杨科技 on 2017/9/23.
//  Copyright © 2017年 饶波. All rights reserved.
//

#import "RBNavigationController.h"

@interface RBNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation RBNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.interactivePopGestureRecognizer.delegate = self;

    // 设置导航条背景图片 
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault]; // Metrics 度量
}


/**
 拦截所有进来的自控制器
 
 @param viewController 自控子器
 @param animated 动画
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.childViewControllers.count > 0) { // 如果viewController 不是最早push进来的控制器
        
        RBNSLogFunction;
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [backBtn setTitle:@"返回" forState:UIControlStateNormal];
        [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [backBtn sizeToFit];
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        
        viewController.hidesBottomBarWhenPushed = YES; // 影藏底部tabbar
    }
    
    // 所有都设置好之后，在push
    [super pushViewController:viewController animated:animated];
}

- (void)back {
    [self popViewControllerAnimated:YES];
}

//- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
//    return [super popViewControllerAnimated:animated];
//}
//
//- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
//    return [super popToViewController:viewController animated:NO];
//}
#pragma mark - <UIGestureRecognizerDelegate>

/**
 UINavigationController 的手势是否好使


 @return YES ： 好使， NO ：不好使
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    // 如果导航控制器只管理一个自控制器，手势不可使。
//    if (self.childViewControllers.count == 1) { 
//        return NO;
//    }
//    return YES;  

    // 手势何时有效：当导航控制器的子控制器个数 >1 时候有效
    return self.childViewControllers.count > 1; 
}



@end
